import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import layout from "../layout/index.vue";
import autoRouter  from "./autoRouter";
import { getToken } from "../utils/Cookie";
import store from "../store";
Vue.use(VueRouter)
console.log(autoRouter)
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  ...autoRouter,
  {
    path: '/404',
    component: () => import('../views/404.vue')
  }
  // {
  //   path: '/',
  //   component: layout,
  //   children:[
  //     {path:'/home',name:'home',component:() => import('../views/Home.vue')},
  //     {path:'/bd',name:'bd',component:() => import('../views/Bd.vue')},
  //     {path:'/test',name:'test',component:() => import('../views/Test.vue')},
  //     {path:'/setting',name:'setting',component:() => import('../views/Setting.vue')},
  //   ]
  // },
  // {
  //   path:'/detail',
  //   name:'detail',
  //   component:() => import('../views/Detail.vue')
  // }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
router.beforeEach(async(to,from,next)=>{
  const token=getToken()
  if(token){
    if(to.path=='/login'){
      next('/home')
    }else{
      try{
        await store.dispatch('user/InfoFun',token)
        next()
      }catch(err){
        next()
      }
    }
  }else{
    next()
  }
})
export default router
